<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/> 后台管理
      </el-breadcrumb-item>

      <el-breadcrumb-item>添加类别</el-breadcrumb-item>

    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="父级类别" prop="parentId">
        <el-input v-model="ruleForm.parentId"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="关键词列表" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon">
        <el-input v-model="ruleForm.icon"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>

      <el-form-item label="是否启用" >
        <el-switch
            v-model="ruleForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#cccccc">
        </el-switch>
      </el-form-item><el-form-item label="是否显示在导航栏" >
        <el-switch
            v-model="ruleForm.isDisplay"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#cccccc">
        </el-switch>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        parentId:'0',
        name: '测试类别001',
        keywords:'测试关键字001',
        icon:'https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png',
        sort:'99',
        enable: 1,
        isDisplay: 1,
      },

      rules: {
        parentId:[
          { required: true, message: '请输入父级类比恶的ID，如果添加一级类别，此处填0', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入相册名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        keywords: [
          { required: true, message: '请输入类别的关键词列表', trigger: 'blur' },
          { min: 2, max: 255, message: '长度在 2 到 255 个字符', trigger: 'blur' }
        ],
        icon:[
          { required: true, message: '请输入类别图标的url', trigger: 'blur' },
          { min: 10, max: 255, message: '长度在 10 到 255 个字符', trigger: 'blur' }
        ],
        sort:[
          { required: true, message: '请输入相册排序序号', trigger: 'blur' },
          { pattern:/^(\d{1}|[1-9]{1}[0-9]?)$/,
            message: '相册序号必须在0~99之间',
            trigger: 'blur'}
        ]

      }
    };
  },
  methods: {
    submitForm(formName) {
      // 执行检查规则（rules）
      this.$refs[formName].validate((valid) => {
        // 满足检查规则➡进if
        if (valid) {
          let url = 'http://localhost:9080/categories/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);
          // 在Vue脚手架中不能再使用function函数
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            if(responseBody.state == 20000){
              this.$message({
                message: '添加类别成功',
                type: 'success'
              });
              this.resetForm(formName);
            }else{
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>

</style>